# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=tk

pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=8.6.1
pkgrel=3
pkgdesc="A windowing toolkit for use with tcl (mingw-w64)"
arch=('any')
url="http://tcl.sourceforge.net"
license=("custom")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-pkg-config")
depends=("${MINGW_PACKAGE_PREFIX}-tcl>=$pkgver")
options=('strip' 'staticlibs')
source=("http://downloads.sourceforge.net/sourceforge/tcl/tk${pkgver}-src.tar.gz"
    002-implib-name.mingw.patch
    003-fix-forbidden-colon-in-paths.mingw.patch
    004-install-man.mingw.patch
    005-fix-redefinition.mingw.patch
    006-prevent-tclStubsPtr-segfault.patch
)
sha1sums=('ecfcc20833c04d6890b14a7920a04d16f2123a51'
          'd58c1fab2909aad185f832235ed3de12d7510feb'
          '5029b27e01826f640fb17daf6f96c13d3f0de293'
          'a96a89cea2823a8f1857885aa8d71d96a829e06b'
          '1a0b29cb8bb21298da118ade70ce21fee398707b'
          'c95d3128d971ecd8ec6e79fcc17e5b0c20f9b666')

prepare() {
  cd "$srcdir/tk$pkgver"
  patch -p1 -i ${srcdir}/002-implib-name.mingw.patch
  patch -p1 -i ${srcdir}/003-fix-forbidden-colon-in-paths.mingw.patch
  patch -p1 -i ${srcdir}/004-install-man.mingw.patch
  patch -p1 -i ${srcdir}/005-fix-redefinition.mingw.patch
  patch -p1 -i ${srcdir}/006-prevent-tclStubsPtr-segfault.patch
  
  cd win && autoreconf -fi
}

build() {
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  cp -rf "$srcdir/tk$pkgver/" "${srcdir}/build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"
  [ "${MINGW_CHOST}" = 'x86_64-w64-mingw32' ] && enable64bit='--enable-64bit'
  "${srcdir}"/${_realname}${pkgver}/win/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --with-tcl=${MINGW_PREFIX}/lib \
    $enable64bit
  make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make install INSTALL_ROOT="$pkgdir"
  cp "${pkgdir}${MINGW_PREFIX}/bin/wish86.exe" "${pkgdir}${MINGW_PREFIX}/bin/wish.exe"
  find "${pkgdir}${MINGW_PREFIX}" -name '*.def' -o -name '*.exp' | xargs -rtl1 rm
  find "${pkgdir}${MINGW_PREFIX}" -name '*.sh' -o -name '*.dll' -o -name '*.exe' | xargs -rtl1 chmod 755

  ln -s "${pkgdir}${MINGW_PREFIX}/lib/libtk86.dll.a" "${pkgdir}${MINGW_PREFIX}/lib/libtk.dll.a"
  mkdir -p "${pkgdir}${MINGW_PREFIX}/include/tk${pkgver%.*}/tk-private/"{generic/ttk,win}
  find generic win -name "*.h" -exec cp -p '{}' "${pkgdir}${MINGW_PREFIX}"/include/tk${pkgver%.*}/tk-private/'{}' ';'
  ( cd "${pkgdir}${MINGW_PREFIX}/include"
    for i in *.h ; do
      cp -f $i ${pkgdir}${MINGW_PREFIX}/include/tk${pkgver%.*}/tk-private/generic/
    done
  )
  chmod a-x "${pkgdir}${MINGW_PREFIX}/lib/"*/pkgIndex.tcl
  
  local _libver=${pkgver%.*}
  _libver=${_libver//./}
  sed -i \
    -e "s|^\(TK_BUILD_LIB_SPEC\)='.*|\1='-Wl,${MINGW_PREFIX}/lib/libtk${_libver}.dll.a'|" \
    -e "s|^\(TK_SRC_DIR\)='.*'|\1='${MINGW_PREFIX}/include/tk${pkgver%.*}/tk-private'|" \
    -e "s|^\(TK_BUILD_STUB_LIB_SPEC\)='.*|\1='-Wl,${MINGW_PREFIX}/lib/libtkstub${_libver}.a'|" \
    -e "s|^\(TK_BUILD_STUB_LIB_PATH\)='.*|\1='${MINGW_PREFIX}/lib/libtkstub${_libver}.a'|" \
    -e "s|^\(TK_STUB_LIB_SPEC\)='.*|\1='-L${MINGW_PREFIX}/lib -ltkstub${_libver}'|" \
    ${pkgdir}${MINGW_PREFIX}/lib/tkConfig.sh

  # Add missing entry to tkConfig.sh
  echo "# String to pass to the compiler so that an extension can" >> ${pkgdir}${MINGW_PREFIX}/lib/tkConfig.sh
  echo "# find installed Tcl headers." >> ${pkgdir}${MINGW_PREFIX}/lib/tkConfig.sh
  echo "TK_INCLUDE_SPEC='-I${MINGW_PREFIX}/include/tk${pkgver%.*}'" >> ${pkgdir}${MINGW_PREFIX}/lib/tkConfig.sh

  rm "${pkgdir}${MINGW_PREFIX}/lib/tk${pkgver%.*}/tkAppInit.c"
}
